const cheerio = require('cheerio');
const qianbi_classify = (html, source) => {
	let bookDetailList = [];
	let $ = cheerio.load(html);
	$('#sitebox dl').each((_, item) => {
		let bookDetail = {
			bookId: $(item).find('dd h3 a').attr('href').split('/')[2],
			bookName: $(item).find('dd h3 a').text(),
			avatar: $(item).find('dt a img').attr('_src'),
			uptime: $(item).find('dd h3 span').text(),
			classify: $(item).find('.book_other span:nth-child(1)').text(),
			bookStatus: $(item).find('.book_other span:nth-child(2)').text(),
			fontNum: $(item).find('.book_other span:nth-child(3)').text(),
			intro: $(item).find('.book_des').text(),
			newChapterName: $(item).find('.book_other a').text(),
			newChapterId: _chapterId($(item).find('.book_other a')),
			source,
		};
		bookDetailList.push(bookDetail);
	});
	return bookDetailList;
};

const abc_classify = (html, source, baseUrl) => {
	let bookDetailList = [];
	let $ = $1(html);
	$('.r ul li').each((i, item) => {
		let bookId = $(item).find('.s2 a').attr('href').split('/')[1];
		let bookIdNum = bookId.split('yt')[1];
		let mulu = 0;
		if (String(bookIdNum).length === 4) {
			mulu = bookIdNum.slice(0, 1);
		} else if (String(bookIdNum).length === 5) {
			mulu = bookIdNum.slice(0, 2);
		}
		let info = {
			bookId,
			bookName: $(item).find('.s2 a').text(),
			avatar: `${baseUrl}files/article/image/${mulu}/${bookIdNum}/${bookIdNum}s.jpg`,
			author: $(item).find('.s5').text(),
			source,
		};
		bookDetailList.push(info);
	});
	return bookDetailList;
};

function $1(html) {
	return cheerio.load(html);
}

function _chapterId(dom) {
	return dom.attr('href').split('/')[3].split('.')[0];
}
module.exports = {
	qianbi_classify,
	abc_classify,
};
